package com.wyy.service.Impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyy.error.BusinessException;
import com.wyy.error.EnumBusinessError;
import com.wyy.mapper.ArticleMapper;
import com.wyy.mapper.UserMapper;
import com.wyy.pojo.Articles;
import com.wyy.pojo.Users;
import com.wyy.pojo.model.ArticleModel;
import com.wyy.pojo.model.ArticleQueryCriteria;
import com.wyy.pojo.model.UserToken;
import com.wyy.service.ArticleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @program: gytcb
 * @author: 吴磊
 * @create: 2021-06-08 11:44
 */
@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public Map<String, Object> getAllArticle(ArticleQueryCriteria articleQueryCriteria) {
        int curPage = articleQueryCriteria.getCurPage();
        HashMap<String, Object> map = new HashMap<>();
        //分页，根据前端传过来的 p 页数
        Page<Articles> page = new Page<>(curPage, 10);
        //总篇数
        long total;
        //总页数
        long pages;
        List<Articles> articles;
        Page<Articles> usersPage = articleMapper.selectPage(page, null);
        pages = usersPage.getPages();
        total = usersPage.getTotal();
        articles = usersPage.getRecords();
        List<ArticleModel> articleModels = convertToArticleModel(articles);
        //总篇数
        map.put("total", total);
        //总页数
        map.put("pages", pages);
        //文章 <=10篇
        map.put("articles", articleModels);
        return map;
    }

    @Override
    public void deleteArticleByArticleId(String articleId) {
        String id = JSON.parseObject(articleId, Articles.class).getArticleId();
        if (StringUtils.isEmpty(id)) {
            throw new BusinessException(EnumBusinessError.PARAMETER_INVALID_ERROR);
        }
        Integer integer = articleMapper.deleteArticleById(id);
        if (integer <= 0) {
            throw new BusinessException(EnumBusinessError.UNKNOWN_ERROR, "帖子删除失败（不要重复此操作）");
        }
    }

    /**
     * 将查出来的文章处理为ArticleModel
     *
     * @param articles
     * @return
     */
    private List<ArticleModel> convertToArticleModel(List<Articles> articles) {
        List<ArticleModel> articleModels = articles.stream().map(article -> {
            ArticleModel articleModel = new ArticleModel();
            BeanUtils.copyProperties(article, articleModel);
            String articleUserId = article.getUserId();
            Users userById = userMapper.getUserById(articleUserId);
            UserToken userToken = new UserToken();
            BeanUtils.copyProperties(userById, userToken);
            articleModel.setUserToken(userToken);
            return articleModel;
        }).collect(Collectors.toList());
        return articleModels;
    }
}
